package cn.elwy.rcp.ui.wizard;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.ISaveablePart2;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorPart;

import cn.elwy.rcp.ui.composite.WizardAreaComposite;
import cn.elwy.rcp.ui.editor.BaseEditorWizard;
import cn.elwy.rcp.ui.listener.CloseListenerAdapter;

public abstract class WizardEditor extends EditorPart implements ISaveablePart2 {

  protected boolean dirty;
  protected BaseEditorWizard wizard;
  protected IEditorInput editorInput = null;
  private WizardAreaComposite wizardAreaComposite;

  public BaseEditorWizard getWizard() {
    return wizard;
  }

  public void setWizard(BaseEditorWizard wizard) {
    this.wizard = wizard;
  }

  public IEditorInput getEditorInput() {
    return editorInput;
  }

  public void setEditorInput(IEditorInput editorInput) {
    this.editorInput = editorInput;
  }

  /**
   * Create contents of the editor part.
   * 
   * @param parent
   */
  @Override
  public void createPartControl(Composite parent) {
    wizardAreaComposite = new WizardAreaComposite(parent, wizard);
    wizardAreaComposite.setCreateHeade(true);
    wizardAreaComposite.setCloseListenerAdapter(new CloseEditorAdapter());
    wizardAreaComposite.createContents();
    wizard.setContainer(wizardAreaComposite);
  }

  @Override
  public void setFocus() {
    wizardAreaComposite.setFocus();
  }

  @Override
  public void doSave(IProgressMonitor monitor) {
  }

  @Override
  public void doSaveAs() {
  }

  @Override
  public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    setSite(site);
    setInput(input);
    editorInput = input;
    initWizard();
  }

  @Override
  public boolean isDirty() {
    return dirty;
  }

  @Override
  public boolean isSaveAsAllowed() {
    return false;
  }

  @Override
  public int promptToSaveOnClose() {
    boolean canClose = wizard.isCanCancel();
    if (!canClose) {
      MessageDialog.openInformation(Display.getDefault().getActiveShell(), "提示", "当前任务未执行结束，无法关闭该窗口!");
      return ISaveablePart2.CANCEL;
    }
    return ISaveablePart2.YES;
  }

  /** 在此方法中初始化 wizard */
  abstract protected void initWizard();

  public void setDirty(boolean dirty) {
    this.dirty = dirty;
    firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
  }

  class CloseEditorAdapter implements CloseListenerAdapter {

    @Override
    public boolean close() {
      IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
      if (wizard != null) {
        if (wizard.isCanSave()) {
          page.saveEditor(WizardEditor.this, true);
        }
      }
      boolean canClose = wizard.isCanCancel();
      if (!canClose) {
        return false;
      }
      if (wizard != null) {
        wizard.dispose();
      }
      return page.closeEditor(WizardEditor.this, false);
    }
  }

}
